<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="用户的主键id" prop="userId">
        <el-input disabled v-model="dataForm.userId" placeholder="用户的主键id"></el-input>
      </el-form-item>
      <el-form-item label="发单人" prop="userNick">
        <el-input v-model="dataForm.userNick" placeholder="用户的昵称"></el-input>
      </el-form-item>
      <el-form-item label="哪一栋" prop="building">
        <el-input v-model="dataForm.building" placeholder="哪一栋"></el-input>
      </el-form-item>
      <el-form-item label="快递类型" prop="type">
        <el-input v-model="dataForm.type" placeholder="快递类型"></el-input>
      </el-form-item>
      <el-form-item label="任务说明" prop="suggest">
        <el-input v-model="dataForm.suggest" placeholder="任务说明"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="多少钱这个单"></el-input>
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-tag v-if="dataForm.status==0">未接</el-tag>
        <el-tag type="success" v-if="dataForm.status==1">已接</el-tag>
        <el-tag type="danger" v-if="dataForm.status==2">已结算</el-tag>
      </el-form-item>
      <el-form-item label="接单人" prop="receiveUserId">
        <el-input v-model="dataForm.receiveTaskUserName" placeholder="接单人"></el-input>
      </el-form-item>
      <el-form-item label="截止时间" prop="endTime">
        <el-input disabled v-model="dataForm.endTime" placeholder="截止时间"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input disabled v-model="dataForm.createTime" placeholder="创建时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        userId: '',
        userNick: '',
        building: '',
        type: '',
        suggest: '',
        price: '',
        status: '',
        receiveUserId: '',
        endTime: '',
        createTime: '',
        receiveTaskUserName: ''
      },
      dataRule: {
        userId: [
          {required: true, message: '用户的主键id不能为空', trigger: 'blur'}
        ],
        userNick: [
          {required: true, message: '用户的昵称不能为空', trigger: 'blur'}
        ],
        building: [
          {required: true, message: '哪一栋不能为空', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '快递类型不能为空', trigger: 'blur'}
        ],
        suggest: [
          {required: true, message: '任务说明不能为空', trigger: 'blur'}
        ],
        price: [
          {required: true, message: '多少钱这个单不能为空', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '0未接 1已接不能为空', trigger: 'blur'}
        ],
        receiveUserId: [
          {required: true, message: '接单人的用户id不能为空', trigger: 'blur'}
        ],
        endTime: [
          {required: true, message: '截止时间不能为空', trigger: 'blur'}
        ],
        createTime: [
          {required: true, message: '创建时间不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/xy/delivery/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.xyDelivery.userId
              this.dataForm.userNick = data.xyDelivery.userNick
              this.dataForm.building = data.xyDelivery.building
              this.dataForm.type = data.xyDelivery.type
              this.dataForm.suggest = data.xyDelivery.suggest
              this.dataForm.price = data.xyDelivery.price
              this.dataForm.status = data.xyDelivery.status
              this.dataForm.receiveUserId = data.xyDelivery.receiveUserId
              this.dataForm.endTime = data.xyDelivery.endTime
              this.dataForm.createTime = data.xyDelivery.createTime
              this.dataForm.receiveTaskUserName = data.xyDelivery.receiveTaskUserName
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/xy/delivery/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'userId': this.dataForm.userId,
              'userNick': this.dataForm.userNick,
              'building': this.dataForm.building,
              'type': this.dataForm.type,
              'suggest': this.dataForm.suggest,
              'price': this.dataForm.price,
              'status': this.dataForm.status,
              'receiveUserId': this.dataForm.receiveUserId,
              'endTime': this.dataForm.endTime,
              'createTime': this.dataForm.createTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
